package FP;

import java.util.Arrays;
import java.util.IntSummaryStatistics;
import java.util.List;
import java.util.Random;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class StreamDemo {
    public static void main(String[] args) {
        // generate
        Random rand = new Random();
        System.out.println("generate 10 random integers: ");
        IntStream stream = rand.ints().limit(10);
        // statistics
        IntSummaryStatistics stats = stream.summaryStatistics();
        System.out.println("max = " + stats.getMax());
        System.out.println("min = " + stats.getMin());
        System.out.println("sum = " + stats.getSum());
        System.out.println("avg = " + stats.getAverage());
        // batch operation
        List<Integer> array = Arrays.asList(2, 3, 3, 2, 5, 2, 7);
        System.out.println("numbers in array are: ");
        array.stream().sorted().forEach(System.out::println);
        List<Integer> squares = array.stream().map(i -> i * i).distinct().collect(Collectors.toList());
        System.out.println("squares of distinct numbers in array = " + squares);
        long evenNumberCount = array.parallelStream().filter(i -> i % 2 == 0).count();
        System.out.println("even number count of array = " + evenNumberCount);
    }
}
